package com.wn.ticket.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.ticket.common.PageBean;
import com.wn.ticket.common.Utils;
import com.wn.ticket.entity.EntranceTicket;
import com.wn.ticket.entity.Perform;
import com.wn.ticket.entity.User;
import com.wn.ticket.mapper.EntranceTicketMapper;
import com.wn.ticket.mapper.PerformAddressMapper;
import com.wn.ticket.mapper.PerformArtistMapper;
import com.wn.ticket.mapper.PerformMapper;
import com.wn.ticket.service.PerformService;
import com.wn.ticket.vo.PerformVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wuqingting
 * @date 2022/8/29
 */
@Service
public class PerformServiceImpl implements PerformService {
    @Autowired
    private PerformMapper performMapper;
    @Autowired
    private PerformArtistMapper performArtistMapper;
    @Autowired
    private EntranceTicketMapper entranceTicketMapper;
    @Autowired
    private PerformAddressMapper performAddressMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Perform perform) {
        if (perform.getPerformAddress().getId()==0){//如果场馆需要新增
            performAddressMapper.add(perform.getPerformAddress());//新增完成会给到自增的id
        }
        performMapper.add(perform);
        performArtistMapper.addList(perform.getId(),perform.getArtistList());//此处使用到了上一步自增的主键,已在xml中配置
        entranceTicketMapper.addList(perform.getId(),perform.getEntranceTicketList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Perform perform) {
        if (perform.getPerformAddress().getId()==0){//如果场馆需要新增
            performAddressMapper.add(perform.getPerformAddress());//新增完成会给到自增的id
        }
        performMapper.update(perform);
        performArtistMapper.delete(perform.getId(),perform.getArtistList());//先删除旧的中间表
        performArtistMapper.addList(perform.getId(),perform.getArtistList());//再添加新的
    }

    @Override
    public List<Perform> getAll() {
        return performMapper.getAll();
    }

    @Override
    public Perform getById(int id) {
        return performMapper.getById(id);
    }

    @Override//本方法用于按最低门票价格对演出排序
    public List<Perform> getOrderByPrice(Perform perform, String order) {
        List<Perform> performList = performMapper.getByCondition(perform);
        for (Perform p:performList){//遍历演出中的所有票价,仅保留最低票价
            List<EntranceTicket> entranceTicketList = p.getEntranceTicketList();
            if (entranceTicketList.size()<=1){
                continue;
            }
            BigDecimal minPrice=entranceTicketList.get(0).getPrice();
            for (int i=0;i<entranceTicketList.size();i++){
                if (minPrice.compareTo(entranceTicketList.get(i).getPrice()) > 0){//比较价格
                    minPrice=entranceTicketList.get(i).getPrice();
                }
            }
            EntranceTicket entranceTicket = new EntranceTicket();
            entranceTicket.setPrice(minPrice);
            p.setEntranceTicketList(Arrays.asList(entranceTicket));//排序完成后仅保留最低价格
        }
        if ("ASC".equalsIgnoreCase(order)) {//价格升序
            performList.sort((p1, p2) -> p1.getEntranceTicketList().get(0).getPrice().subtract(p2.getEntranceTicketList().get(0).getPrice()).intValue());
        }else if ("DESC".equalsIgnoreCase(order)){//价格降序
            performList.sort((p1, p2) -> p2.getEntranceTicketList().get(0).getPrice().subtract(p1.getEntranceTicketList().get(0).getPrice()).intValue());
        }
        return performList;
    }

    @Override
    public PageBean<Perform> getPageByCondition(Perform perform, int startPage, int pageSize) {
        Page<Object> page = PageHelper.startPage(startPage, pageSize);
        List<Perform> list = performMapper.getByCondition(perform);
        PageBean<Perform> pageBean = Utils.getPageBean(Perform.class, page, list, startPage, pageSize);
        return pageBean;
    }

    @Override
    public List<Perform> getByHot() {
        return performMapper.getByHot();
    }

    @Override
    public Perform getId(int id) {
        return performMapper.getById(id);
    }

    @Override
    public List<Perform> getByCity(String name) {
        return performMapper.getByCity(name);
    }

    @Override
    public List<Perform> getByProfile(String profile) {
        return performMapper.getByProfile(profile);
    }

    @Override
    public List<Perform> getByArName(String arName) {
        return performMapper.getByArName(arName);
    }

}
